//
//  VisitPatientInfoVC.m
//  Doctor
//
//  Created by 阳光 on 16/9/7.
//  Copyright © 2016年 阳光. All rights reserved.
//  就诊病例详情界面

#import "VisitPatientInfoVC.h"
#import "VisitDocCell.h"
#import "FirstPatientInfoVC.h"
#import "VisitPatientInfoVC.h"
#import "AddDocAdviceVC.h"
#import "PatientInfoCell.h"
#import "PatientBaseInfoCell.h"
#import "PatientHealthInfoCell.h"
#import "PatientBabyInfoCell.h"
#import "PatientOtherInfoCell.h"
#import "VisitDocAdviceCell.h"
#import "VisitDocAdviceListModel.h"


#import "HusbandInfoVC.h"
#import "SickInfoVC.h"
#import "DoBornBaby.h"
#import "PurposeVC.h"

#import "SectionGroupModel.h"
#import "SectionListHeader.h"

static NSString *visitDocCellIdentifier=@"VisitDocCellIdentifier";
static NSString *visitDocAdviceCellIdentifier=@"VisitDocAdviceCellIdentifier";

static NSString *patientInfoCellIdentifier=@"PatientInfoCellIdentifier";
static NSString *patientBaseInfoCellIdentifier=@"PatientBaseInfoCellIdentifier";
static NSString *patientHealthInfoCellIdentifier=@"PatientHealthInfoCellIdentifier";
static NSString *patientBabyInfoCellIdentifier=@"PatientBabyInfoCellIdentifier";
static NSString *patientOtherInfoCellIdentifier=@"PatientOtherInfoCellIdentifier";
static NSString *tableViewCellIdentifier=@"UITableViewCellIdentifier";

@interface VisitPatientInfoVC ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate,PatientOtherInfoCellDelegate,SectionListHeaderDelegate>

@property(nonatomic,strong)UILabel *downLine;

// 病例
@property(nonatomic,strong)UITableView *patientInfoTableView;

// 医嘱
@property(nonatomic,strong)UITableView *visitDocTableView;

@property(nonatomic,strong)UIButton *addBtn;

@property(nonatomic,strong)NSDictionary *patientInfoDic;

@property(nonatomic,strong)NSMutableArray *visitDocAdviceArr;

@property(nonatomic,strong)NSMutableArray *sectionArr;

@end


@implementation VisitPatientInfoVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor=WhiteColor;
    
    _visitDocAdviceArr=[NSMutableArray array];
    
    _sectionArr=[NSMutableArray array];
    
    
    NSArray *sectionName= @[NSLocalizedString(@"Unsubmitted", nil), NSLocalizedString(@"Submitted", nil)];//@[@"未提交",@"已提交"];
    
    for (NSInteger i=0;i<sectionName.count;i++)
    {
        SectionGroupModel *sectionGroupModel=[SectionGroupModel new];
        sectionGroupModel.name=sectionName[i];
        [_sectionArr addObject:sectionGroupModel];
    }
    
    self.title = NSLocalizedString(@"Treatment", nil);//就诊;//@"病例详情";
    
    [self initNavBarBtnItem];
    
    [self initSelectView];
    
    [self initTableView];
    
    [self initBottomView];
    
    [self get];
    
   
    
}

-(void)get
{
    NSDictionary *dic=@{
                        @"doctor_id":[Util getUserDefaultObjectForKey:@"id"],
                        @"user_id":@"1"
                        };
    
    [QBRequest GetPatientInfoWithDic:dic Success:^(id res)
    {
        _patientInfoDic=res[@"caseInfo"];
        
        [_patientInfoTableView reloadData];
        
    } failure:^(NSString *error)
    {
        
    }];
    
}

-(void)getVisitPateintDocAdviceData
{
    
    NSDictionary *dic=@{
                        @"doctor_id":[Util getUserDefaultObjectForKey:@"id"],
                        @"user_id":_patientID,
                        @"type":@"0"
                        };
    
    __weak typeof(self) weakSelf=self;
    [QBRequest GetDoctorAdviceVisitPatientWithDic:dic Success:^(id res)
     {
         //NSLog(@"6666660000111--%@  --%@  ",res,res[@"info"]);
         if ([res[@"info"]count]>0)
         {
             weakSelf.visitDocAdviceArr=[VisitDocAdviceListModel mj_objectArrayWithKeyValuesArray:res[@"info"]];
         }
         else
         {
             //NSLog(@"message88888 :%@", res[@"message"]);
             if ([res[@"message"] isEqualToString:@"患者医嘱信息为空"]) {//..
                 [self.view makeToast:NSLocalizedString(@"No advices", nil) duration:1.0 position:CSToastPositionCenter];
             } else {
                [self.view makeToast:res[@"message"] duration:1.0 position:CSToastPositionCenter];
             }
             
         }
         
         [weakSelf.visitDocTableView reloadData];

     } failure:^(NSString *error)
     {
         
     }];
}

-(void)initBottomView
{
    // 添加医嘱
    _addBtn=[UIButton new];
    _addBtn.hidden=YES;
    [self.view addSubview:_addBtn];
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.bottom.mas_equalTo(-kmargin);
        make.size.mas_equalTo(CGSizeMake(APPW-kmargin*2,kmargin*4));
    }];
    
    ViewBorderRadius(_addBtn,kmargin/2,2,ClearColor);
    _addBtn.backgroundColor=Main_Color;
    _addBtn.titleLabel.font=Font_Number(16);
    [_addBtn setTitle:NSLocalizedString(@"Add advice", nil) forState:0];//@"添加医嘱"
    [_addBtn setTitleColor:WhiteColor forState:0];
    [_addBtn addTarget:self action:@selector(addBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)addBtnEvent
{
    // 添加医嘱界面跳转
    AddDocAdviceVC *addDocAdviceVC=[AddDocAdviceVC new];
    addDocAdviceVC.patientID=_patientID;
    [self.navigationController pushViewController:addDocAdviceVC animated:NO];
}

-(void)initNavBarBtnItem
{
    self.navigationItem.leftBarButtonItem=[UIBarButtonItem initWithNormalImage:@"back_gray" target:self action:@selector(backItemEvent)];
}

-(void)backItemEvent
{
    [self.navigationController popViewControllerAnimated:NO];
}

-(void)initTableView
{
    // _patientInfoTableView
    _patientInfoTableView=[[UITableView alloc]initWithFrame:CGRectMake(0,selectViewH,APPW,APPH-selectViewH-64) style:UITableViewStylePlain];
    _patientInfoTableView.delegate=self;
    _patientInfoTableView.dataSource=self;
    _patientInfoTableView.hidden=NO;
    _patientInfoTableView.showsVerticalScrollIndicator=NO;
    _patientInfoTableView.backgroundColor=WhiteColor;
    _patientInfoTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:_patientInfoTableView];
    [_patientInfoTableView registerClass:[PatientInfoCell class] forCellReuseIdentifier:patientInfoCellIdentifier];
    [_patientInfoTableView registerClass:[PatientBaseInfoCell class] forCellReuseIdentifier:patientBaseInfoCellIdentifier];
    [_patientInfoTableView registerClass:[PatientHealthInfoCell class] forCellReuseIdentifier:patientHealthInfoCellIdentifier];
    [_patientInfoTableView registerClass:[PatientBabyInfoCell class] forCellReuseIdentifier:patientBabyInfoCellIdentifier];
    [_patientInfoTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:tableViewCellIdentifier];
    
    
    // _patientInfoTableView
    _visitDocTableView=[[UITableView alloc]initWithFrame:CGRectMake(0,selectViewH,APPW,APPH-selectViewH-64-kmargin*6) style:UITableViewStylePlain];
    _visitDocTableView.delegate=self;
    _visitDocTableView.dataSource=self;
    _visitDocTableView.hidden=YES;
    _visitDocTableView.showsVerticalScrollIndicator=NO;
    _visitDocTableView.backgroundColor=MAIN_BACK_COLOR;
    _visitDocTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:_visitDocTableView];
    _visitDocTableView.tableFooterView=[UIView new];
    [_visitDocTableView registerClass:[VisitDocAdviceCell class] forCellReuseIdentifier:visitDocAdviceCellIdentifier];
    
//     [_visitDocTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Lester_lz@163.com"];
    
}


-(void)initSelectView
{
    //标题
    UIView *titleView=[[UIView alloc]initWithFrame:CGRectMake(0,1,APPW,selectViewH)];
    titleView.backgroundColor=WhiteColor;
    [self.view addSubview:titleView];
    
    
    
    NSArray *titleArr=@[NSLocalizedString(@"Patient Case", nil), NSLocalizedString(@"Advices", nil)];//@[@"病例",@"医嘱"];
    
    for (int i =0;i<titleArr.count;i++) {
        UIButton *titleArrBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        titleArrBtn.frame = CGRectMake(APPW/2.0*i,2.5,APPW/2.0,40);
        [titleArrBtn setTitle:titleArr[i] forState:UIControlStateNormal];
        [titleArrBtn setTitleColor:Color333333 forState:UIControlStateNormal];
        [titleArrBtn setTitleColor:Main_Color forState:UIControlStateSelected];
        titleArrBtn.titleLabel.font=Font_Number(16);
        [titleArrBtn addTarget:self action:@selector(titleArrBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
        titleArrBtn.tag=10+i;
        
        _downLine=[UILabel new];
        //[[UILabel alloc]initWithFrame:CGRectMake(50,42,APPW/2.0-100,2)];
        _downLine.backgroundColor=Main_Color;
        [titleView addSubview:_downLine];
        
        if(i==0)
        {
            titleArrBtn.selected=YES;
        }
        [titleView addSubview:titleArrBtn];
    }
    
    // 中线
    UIView *midLine=[[UIView alloc]initWithFrame:CGRectMake(APPW/2.0-0.5,10,1.0,25)];
    midLine.backgroundColor=Color999999;
    [titleView addSubview:midLine];
    
    UIView *bottomLine=[[UIView alloc]initWithFrame:CGRectMake(0,selectViewH-2,APPW,1)];
    bottomLine.backgroundColor=MAIN_BACK_COLOR;
    [titleView addSubview:bottomLine];
    
    _downLine=[[UILabel alloc]initWithFrame:CGRectMake(50,42,APPW/2.0-100,2)];
    _downLine.backgroundColor=Main_Color;
    [titleView addSubview:_downLine];
    
}

-(void)titleArrBtnEvent:(UIButton *)sender
{
    for (int i=0; i<2;i++)
    {
        UIButton *button=(UIButton *)[self.view viewWithTag:10+i];
        if (button.tag!=sender.tag)
        {
            button.selected=NO;
        }
        else
        {
            button.selected=YES;
        }
    }
    
    if (sender.tag==10)
    {
        _addBtn.hidden=YES;
        _visitDocTableView.hidden=YES;
        _patientInfoTableView.hidden=NO;
        _downLine.frame=CGRectMake(50,42,APPW/2.0-100,2);
    }
    else
    {
        [self getVisitPateintDocAdviceData];
        
        _addBtn.hidden=NO;
        _visitDocTableView.hidden=NO;
        _patientInfoTableView.hidden=YES;
        _downLine.frame=CGRectMake(APPW/2.0+50,42,APPW/2.0-100,2);
    }
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView==_patientInfoTableView)
    {
        return 5;
    }
    else
    {
        return self.sectionArr.count;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==_patientInfoTableView)
    {
        if (section==4)
        {
            return 4;
        }
        else
        {
            return 1;
        }
    }
    else
    {
        SectionGroupModel *sectionGroupModel=self.sectionArr[section];
        return sectionGroupModel.isOpen?self.visitDocAdviceArr.count:0;
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==_patientInfoTableView)
    {
        if (indexPath.section==0)
        {
            PatientInfoCell *cell=[tableView dequeueReusableCellWithIdentifier:patientInfoCellIdentifier];
            cell.visitDocListModel=_visitDocListModel;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            return cell;
        }
        else  if (indexPath.section==1)
        {
            PatientBaseInfoCell *cell=[tableView dequeueReusableCellWithIdentifier:patientBaseInfoCellIdentifier];
            cell.patientInfoDic=_patientInfoDic;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            return cell;
        }
        else if (indexPath.section==2)
        {
            PatientHealthInfoCell *cell=[tableView dequeueReusableCellWithIdentifier:patientHealthInfoCellIdentifier];
            cell.patientInfoDic=_patientInfoDic;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            return cell;
        }
        else if (indexPath.section==3)
        {
            PatientBabyInfoCell *cell=[tableView dequeueReusableCellWithIdentifier:patientBabyInfoCellIdentifier];
            cell.patientInfoDic=_patientInfoDic;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            return cell;
        }
        else
        {
            UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];
            if (indexPath.row==0)
            {
                cell.textLabel.text = NSLocalizedString(@"Husband Fertility", nil);//@"丈夫生育状况";
            }
            else if (indexPath.row==1)
            {
                cell.textLabel.text = NSLocalizedString(@"Infertility Treatment", nil);//@"不孕治疗";
            }
            else if (indexPath.row==2)
            {
                cell.textLabel.text = NSLocalizedString(@"ART History", nil);//@"您做过几次助孕治疗周期";
            }
            else
            {
                cell.textLabel.text = NSLocalizedString(@"Purpose of IVF", nil);//@"您悬着试管的目的";

            }
            cell.textLabel.font=Font_Number(16);
            cell.textLabel.textColor=Color666666;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            return cell;
        }
    }
    else
    {
        VisitDocAdviceCell *cell=[tableView dequeueReusableCellWithIdentifier:visitDocAdviceCellIdentifier];
        cell.visitDocAdviceListModel=self.visitDocAdviceArr[indexPath.row];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
        
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==_patientInfoTableView)
    {
        if (indexPath.section==0)
        {
            return 50;
        }
        else  if (indexPath.section==1)
        {
           return 44*7;
        }
        else if (indexPath.section==2)
        {
            return 44*9;
        }
        else if (indexPath.section==4)
        {
            return 44;
        }
        else
        {
            return 44*4;
        }
        
    }
    else
    {
        return 220/2;
    }
}

-(void)husbandBtnEvent
{
    HusbandInfoVC *husbandInfoVC=[HusbandInfoVC new];
    husbandInfoVC.patientInfoDic=_patientInfoDic;
    [self.navigationController pushViewController:husbandInfoVC animated:NO];
}

-(void)sickBtnEvent
{
    SickInfoVC *sickInfoVC=[SickInfoVC new];
    sickInfoVC.patientInfoDic=_patientInfoDic;
    [self.navigationController pushViewController:sickInfoVC animated:NO];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (indexPath.section==4)
    {
        // 丈夫生育治疗
        if (indexPath.row==0)
        {
            HusbandInfoVC *husbandInfoVC=[HusbandInfoVC new];
            husbandInfoVC.patientInfoDic=_patientInfoDic;
            [self.navigationController pushViewController:husbandInfoVC animated:NO];
        }
        // 不孕治疗
        else if (indexPath.row==1)
        {
            SickInfoVC *sickInfoVC=[SickInfoVC new];
            sickInfoVC.patientInfoDic=_patientInfoDic;
            [self.navigationController pushViewController:sickInfoVC animated:NO];
        }
        // 助孕治疗周期
        else if (indexPath.row==2)
        {
            DoBornBaby *doBornBaby=[DoBornBaby new];
            doBornBaby.patientInfoDic=_patientInfoDic;
            [self.navigationController pushViewController:doBornBaby animated:NO];
        }
        // 悬着试管的目的
        else
        {
            PurposeVC *purposeVC=[PurposeVC new];
            purposeVC.patientInfoDic=_patientInfoDic;
            [self.navigationController pushViewController:purposeVC animated:NO];
        }
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView==_patientInfoTableView)
    {
        if (section==1||section==2||section==3||section==4)
        {
            return 45;
        }
        else
        {
            return 0.000000;
        }
    }
    else
    {
        return 45;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (tableView==_patientInfoTableView)
    {
        if (section==1)
        {
            UIView *headView=[UIView new];
            headView.backgroundColor=MAIN_BACK_COLOR;
            
            UILabel *userLabel=[headView getLabelWithText:NSLocalizedString(@"Basic Information", nil) font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];//@"基本信息"
            [headView addSubview:userLabel];
            [userLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.mas_equalTo(kmargin);
            }];
            return headView;
        }
        else if (section==2)
        {
            UIView *headView=[UIView new];
            headView.backgroundColor=MAIN_BACK_COLOR;
            //女性健康的基本状况
            UILabel *userLabel=[headView getLabelWithText:NSLocalizedString(@"Physical Condition", nil) font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
            [headView addSubview:userLabel];
            [userLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.mas_equalTo(kmargin);
            }];
            return headView;
        }
        else if (section==3)
        {
            UIView *headView=[UIView new];
            headView.backgroundColor=MAIN_BACK_COLOR;
            //@"女性生育史"
            UILabel *userLabel=[headView getLabelWithText:NSLocalizedString(@"Fertility History", nil) font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
            [headView addSubview:userLabel];
            [userLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.mas_equalTo(kmargin);
            }];
            return headView;
        }
        else
        {
            UIView *headView=[UIView new];
            headView.backgroundColor=MAIN_BACK_COLOR;
            //其他
            UILabel *userLabel=[headView getLabelWithText:NSLocalizedString(@"Others", nil) font:Font_Number(16) textColor:Color666666 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
            [headView addSubview:userLabel];
            [userLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.mas_equalTo(kmargin);
            }];
            return headView;
        }
    }
    else
    {
        SectionListHeader *sectionListHeader=[SectionListHeader headerView:tableView];
        sectionListHeader.delegate=self;
        sectionListHeader.sectionGroupModel=self.sectionArr[section];
        sectionListHeader.contentView.backgroundColor=WhiteColor;
        return sectionListHeader;
    }
}

// SectionListHeaderDelegate
-(void)didSelectTableViewHeaderFooterView
{
    [self.visitDocTableView reloadData];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (tableView==_patientInfoTableView)
    {
        if (section==4)
        {
            UIView *footView=[UIView new];
            return footView;
        }
        else
        {
            return nil;
        }
    }
    else
    {
        return nil;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (tableView==_patientInfoTableView)
    {
        if (section==4)
        {
            return kmargin*3;
        }
        else
        {
            return 0.00000;
        }
    }
    else
    {
        return 0.0000;
    }
}

// 禁止tableview滑动时header不悬浮
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView ==_patientInfoTableView)
    {
        CGFloat sectionHeaderHeight=45;
        if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0)
        {
            scrollView.contentInset=UIEdgeInsetsMake(-scrollView.contentOffset.y,0,0,0);
        }
        else if (scrollView.contentOffset.y>=sectionHeaderHeight)
        {
            scrollView.contentInset=UIEdgeInsetsMake(-sectionHeaderHeight,0,0,0);
        }
    }
}

// 间隙置边
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)])
    {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)])
    {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}


@end
